package portal.mq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;

import portal.beans.Node;
import portal.common.ShellExec;

//@Component
public class MQProducer {
	//@Autowired
	private RabbitTemplate rabbitTemplate = (RabbitTemplate) ContextLoader.getCurrentWebApplicationContext().getBean("rabbitTemplate");
	private Logger logger= LoggerFactory.getLogger(MQProducer.class);
	
//	@Value("rout")
//	String routingKey;
	private static MQProducer instance=new MQProducer();
	private void sendMessageMQ(String data)  {
			if(rabbitTemplate==null){
				logger.error("Empty");
				return ;
			}
			//ShellExec.callShell("pwd");
			sendFile(data);
         //rabbitTemplate.send("cetc.fanout.exchange", "", new Message(data.getBytes(), new MessageProperties()));
//            rabbitTemplate.convertAndSend("")
    }
	private void sendMessageMQ(Node node){
		MessageInfo mes=sendProcess(node);
		if(mes!=null){
			rabbitTemplate.convertAndSend("cetc.fanout.exchange",null,SerializationUtils.serialize(mes));
			logger.error("message sended : "+mes);
		}
		else
			logger.error("invaid node Ip or Process Limit");
	}
	public MessageInfo sendFile(String fileName){
		 	  File file = new File(fileName);  
	        Long filelength = file.length(); 
	        MessageInfo mes=new MessageInfo();
	        byte[] filecontent = new byte[filelength.intValue()];  
	        try {  
	            FileInputStream in = new FileInputStream(file);  
	            in.read(filecontent);  
	            in.close(); 
	            	mes.setBlockNo(1);
	            	mes.setData(filecontent);
	            	mes.setFileLenth(filelength.intValue());
	            	mes.setTotalBolck(1);
	            	mes.setFileName(fileName);
	            	rabbitTemplate.convertAndSend("cetc.fanout.exchange",null,SerializationUtils.serialize(mes));
	            	return mes;
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	        return null;
	        
	}
	public MessageInfo sendProcess(Node node){
		if(node.getNodeIp()==null 
				||node.getLimitProcess()==null
				||node.getLimitProcess().length()==0)
			return null;
		MessageInfo mes=new MessageInfo();
		node.getNodeIp();
		mes.setTopic(node.getNodeIp());
		mes.setBlockNo(1);
		mes.setTotalBlock(1);
		mes.setData(node.getLimitProcess().getBytes());
		return mes;
	}
	public static MQProducer getInstance(){
		synchronized(instance){
			if(instance==null)
				instance=new MQProducer(); 
		}
		return instance;
	}
	public static void sendMessage(String fileName){
		getInstance().sendMessageMQ(fileName.replaceAll("//", "/"));
	}
	public static void sendMessage(Node node){
		getInstance().sendMessageMQ(node);
	}
	public RabbitTemplate getRabbitTemplate() {
		return rabbitTemplate;
	}
	public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
	}
	
}
